<template>
  <view class="share-section">
    <view class="share-content">
      <view class="close-section" @click="closeShare">
        <i class="iconfont icon-arrow-down"></i>
        <view>{{$t('common.close')}}</view>
      </view>
      <view class="to-friend">{{$t('coupon.share')}}</view>
      <view class="poster-img">
        <image
          class="share-img"
          v-if="imgshow"
          :src="img_src"
          mode=""
          show-menu-by-longpress
          @click="previewImg"
        />
        <view class="download-box" v-if="imgshow" @click="saveEwm">
          <i class="iconfont icon-download"></i>
          <button
            v-if="!openSettingBtnHidden"
            class="open-btn"
            hover-class="none"
            open-type="openSetting"
            @opensetting="handleSetting"
          >
            {{ $t("common.album") }}
          </button>
        </view>
      </view>
      <view class="poster-down-text">
        <view class="line"></view>
        <view class="text">{{$t('coupon.pic')}} </view>
        <view class="line"></view>
      </view>
      <view class="safe"></view>
    </view>
    <view class="share-bg" @click="closeShare">
      <view class="grey-bg"></view>
      <view class="white-bg"></view>
    </view>
  </view>
</template>

<script>
export default {
  name: "",
  props: {
    couponID: {
      type: String,
      default: "",
    },
  },
  mixins: [],
  components: {},
  data() {
    return {
      appShare: [
        {
          type: this.$t("common.shareFriend"),
          pic_url: "share_wx",
          id: 1,
        },
        {
          type: this.$t("common.beforePoster"),
          pic_url: "share_qrcode",
          id: 2,
        },
      ],
      show: false,
      poster_show: false,
      imgshow: false,
      img_src: "",
      openSettingBtnHidden: true, //是否授权
    };
  },
  mounted() {
    this.generate();
  },
  watch: {},
  computed: {},
  methods: {
    closeShare() {
      this.$emit("closeShare");
    },
    handleToShare(item) {
      if (item.type == "wexin") {
        this.shareWx();
      }
      if (item.type == "wxgroup") {
        this.shareTo("WXSenceTimeline");
      }
    },
    shareTo(type) {
      switch (type) {
        case "WXSceneSession":
          uni.share({
            provider: "weixin",
            scene: "WXSceneSession",
            type: 2,
            imageUrl: this.img_src
          });
          break;
        case "WXSenceTimeline":
          uni.share({
            provider: "weixin",
            scene: "WXSenceTimeline",
            type: 2,
            imageUrl: this.img_src
          });
          break;
        default:
          break;
      }
    },
    onAppShare(type) {
      switch (id) {
        case 2:
          this.generate();
          break;
        case 1:
          this.shareWx();
          break;
        default:
          break;
      }
    },
    //微信小程序保存到相册
    handleSetting(e) {
      if (!e.detail.authSetting["scope.writePhotosAlbum"]) {
        this.openSettingBtnHidden = false;
      } else {
        this.openSettingBtnHidden = true;
      }
    },
    //获取授权
    saveEwm(e) {
      var _self = this;
      //获取相册授权
      uni.getSetting({
        success(res) {
          if (!res.authSetting["scope.writePhotosAlbum"]) {
            uni.authorize({
              scope: "scope.writePhotosAlbum",
              success() {
                //这里是用户同意授权后的回调
                _self.saveImgToLocal();
              },
              fail() {
                //这里是用户拒绝授权后的回调
                _self.openSettingBtnHidden = false;
              },
            });
          } else {
            //用户已经授权过了
            _self.saveImgToLocal();
          }
        },
      });
    },
    //下载并保存图片
    saveImgToLocal(e) {
      let that = this;
      var _self = this;
      uni.showModal({
        title: that.$t("order.prompt"),
        content: that.$t("common.shareContent"),
        success: function (res) {
          if (res.confirm) {
            uni.downloadFile({
              url: _self.img_src, //图片地址
              success: (res) => {
                if (res.statusCode === 200) {
                  uni.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: function () {
                      uni.showToast({
                        title: that.$t("common.saveSuc"),
                        icon: "none",
                      });
                    },
                    fail: function (err) {
                      uni.showToast({
                        title: that.$t("common.saveFail"),
                        icon: "none",
                      });
                    },
                  });
                }
              },
            });
          } else if (res.cancel) {
          }
        },
      });
    },
    showShare() {
      this.show = true;
    },
    //生成海报
    generate() {
      this.show = false;
      this.poster_show = true;
      var params = {
        goods_id: this.topic_id,
      };
      this.$allrequest.goods
        .goodsQrcode(params)
        .then((res) => {
          if (res.code == 0) {
            this.img_src = res.data.pic_url;
            this.imgshow = true;
          }
        })
        .catch((error) => {
          this.$u.toast(this.$t("common.posterFail"));
          this.poster_show = false;
        });
    },
    // app分享微信朋友
    shareWx() {
      uni.share({
        provider: "weixin",
        type: 5,
        miniProgram: {
          id: "gh_c4b415c192a2",
          path: "/goods/goods/goods?id=" + this.topic_id,
          type: 2,
        }
      });
    },
    // 点击预览图片
    previewImg() {
      uni.previewImage({
        current: "0",
        urls: [this.img_src],
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.share-section {
  width: 100%;
  height: 100vh;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 121;
  .share-bg {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    .grey-bg {
      width: 100%;
      height: 63.48vh;
      background: #000000;
      opacity: 0.5;
    }
    .white-bg {
      flex: 1;
      background-color: #f2f2f2;
    }
  }
  .share-content {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 12.665vw;
    top: 0;
    z-index: 11;
    display: flex;
    flex-direction: column;
    width: 74.67vw;
    .safe {
      padding-bottom: env(safe-area-inset-bottom);
    }
    .close-section {
      padding-top: 1vw;
      display: flex;
      justify-content: center;
      flex-direction: column;
      align-items: center;
      .iconfont {
        color: #ffffff;
        opacity: 0.3;
      }
      view {
        font-size: 26rpx;
        font-family: PingFang SC;
        font-weight: 600;
        color: #ffffff;
        margin-top: 1.067vw;
        opacity: 0.3;
      }
    }
    .to-friend {
      margin-top: 1.34vw;
      text-align: center;
      font-size: 40rpx;
      font-family: PingFang SC;
      font-weight: 600;
      color: #ffffff;
    }
    .poster-img {
      width: 74.67vw;
      height: 101.33vw;
      background: #ffffff;
      box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.05);
      opacity: 1;
      border-radius: 2.67vw;
      margin: 2.67vw auto 4vw;
      position: relative;
      overflow: hidden;
      .share-img {
        width: 100%;
        height: 100%;
        display: block;
      }
      .download-box {
        width: 8.8vw;
        height: 8.8vw;
        border-radius: 50%;
        background-color: rgba(0, 0, 0, 0.7);
        display: flex;
        justify-content: center;
        align-items: center;
        position: absolute;
        right: 3.2vw;
        top: 3.73vw;
        z-index: 11;
        .iconfont {
          color: #ffffff;
          font-size: 44rpx;
        }
        .open-btn {
          width: 100%;
          height: 100%;
          opacity: 0;
          position: absolute;
          left: 0;
          top: 0;
          z-index: 12;
        }
      }
    }

    .poster-down-text {
      position: relative;
      font-size: 24rpx;
      font-family: PingFang SC;
      font-weight: 400;
      color: #8c8c8c;
      display: flex;
      justify-content: center;
      align-items: center;
      .line {
        width: 96rpx;
        height: 2rpx;
        background: #ebebeb;
      }
      .text {
        margin: 0 32rpx;
      }
    }

    .share-btn-group {
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-top: 4vw;
      .share-btn-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        margin-right: 5.33vw;
        position: relative;
        .item-icon-box {
          width: 12.8vw;
          height: 12.8vw;
          border-radius: 50%;
          background-color: #ffffff;
          display: flex;
          justify-content: center;
          align-items: center;
          .iconfont {
            font-size: 45rpx;
          }
        }
        .item-text {
          font-size: 22rpx;
          font-family: PingFang SC;
          font-weight: 400;
          color: #262626;
          margin-top: 2.13vw;
        }
        .wxshareBtn {
          width: 100%;
          height: 100%;
          position: absolute;
          left: 0;
          top: 0;
          z-index: 11;
          opacity: 0;
        }
      }
      .share-btn-item:last-child {
        margin-right: 0;
      }
    }
  }
}
</style>
